﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Optimization
{
    /// <summary>
    /// This class represents an abstract layer for optimizer classes.
    /// </summary>
    [Serializable]
    public abstract class AbstractOptimizer
    {
        /// <summary>
        /// Describes if the calculated extremum has a precision error.
        /// </summary>
        protected bool precisionError;

        /// <summary>
        /// The number of needed iteration to find the extremum.
        /// </summary>
        protected int neededIterations;

        /// <summary>
        /// The relative error of the result.
        /// </summary>
        protected double relativeError;

        /// <summary>
        /// The function from which an extremum should be found.
        /// </summary>
        private IRealFunction function;

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractOptimizer"/> class.
        /// </summary>
        /// <param name="function">The function from which an extremum should be found.</param>
        protected AbstractOptimizer(IRealFunction function)
        {
            if (function == (IRealFunction) null)
            {
                throw new ArgumentNullException("function");
            }

            this.function = function;
        }

        /// <summary>
        /// Describes if the calculated extremum has a precision error. If true the
        /// calculated extremum may not be correct.
        /// </summary>
        /// <value><c>True</c> if [precision error]; otherwise, <c>false</c>.</value>
        public bool PrecisionError
        {
            get { return precisionError; }
        }

        /// <summary>
        /// The number of needed iteration to find the extremum.
        /// </summary>
        /// <value>The needed iterations to find the extremum.</value>
        public int NeededIterations
        {
            get { return neededIterations; }
        }

        /// <summary>
        /// Gets the relative error of the result.
        /// </summary>
        /// <value>The relative error of the result.</value>
        public double RelativeError
        {
            get { return relativeError; }
        }

        /// <summary>
        /// Gets or sets the function from which an extremum should be found.
        /// </summary>
        /// <value>The function from which an extremum should be found.</value>
        public IRealFunction Function
        {
            get { return function; }
            set { function = value; }
        }
    }
}